const query = require('../db/query')

// 获取药品列表
const getRestorativeList = (params)=>{
    const data = {
        pageCode:params.pageCode ? params.pageCode : 1,
        pageSize:params.pageSize ? params.pageSize : 10,
        state:params.state ? params.state : '',
        search:params.search ? params.search : ''
    }
    let sql = `SELECT * FROM restoratives WHERE restorativesstate like '%${data.state}%' and restorativesname like '%${data.search}%' limit ${(data.pageCode - 1) * data.pageSize}, ${data.pageSize} `
    return query(sql)
}

// 获取药品分类
const getRestorativeType = (params)=>{
    let sql = `select * from restorativesstate`
    return query(sql)
}

// 获取药品详情
const getRestorativeDetail = (params)=>{
    let sql = `select * from restoratives where restorativesid = ${params.id}`
    return query(sql)
}

// 删除药品
const deleteRestorative = (params)=>{
    let sql = `delete from restoratives where restorativesid = ${params.id}`
    return query(sql)
}

// 新增药品
const restorativeAdd = (params)=>{
    let sql = `INSERT INTO restoratives (restorativesname,restorativesprice,restorativescount,restorativesinven,restorativesstate,restorativestitle,restorativesimage,restorativesdesc) VALUES ('${params.name}','${params.price}','${params.count}','${params.inven}','${params.state}','${params.title}','${params.image}','${params.desc}')`
    return query(sql)
}

// 编辑药品
const restorativeEdit = (params)=>{
    let sql = `UPDATE restoratives SET restorativesname = '${params.name}', restorativesprice = '${params.price}', restorativescount = '${params.count}', restorativesinven = '${params.inven}', restorativesstate = '${params.state}', restorativestitle = '${params.title}', restorativesimage = '${params.image}', restorativesdesc = '${params.desc}' WHERE restorativesid = ${params.id}`
    return query(sql)
}

module.exports = {
    getRestorativeList,
    getRestorativeType,
    getRestorativeDetail,
    deleteRestorative,
    restorativeAdd,
    restorativeEdit
}